﻿using System;
using ArtMath.Resource;
using ArtMath.Component.Point;
using ArtMath.Attribute;
using ArtMath.Core.Helper;
using ArtMath.Core.Data;
using ArtMath.PointCollection;

namespace ArtMath.Component.Circle
{
    /// <summary>
    /// 三点圆
    /// </summary>
    [Serializable]
    public class ThreePointsCircle : FullCircleBase
    {
        public ThreePointsCircle(SketchPad pad, string name, bool addToList = true) : base(pad, name, ToolTypeName.sdy, addToList)
        {
            InPoints = new PointBase[3];
        }

        [MultilingualCategory("Structure"), MultilingualDisplayName("{0}1", "PointOnCircle")]
        public PointBase PointOnCircle1
        {
            get { return InPoints[0]; }
            set { SetPoint(0, value); }
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("{0}2", "PointOnCircle")]
        public PointBase PointOnCircle2
        {
            get { return InPoints[1]; }
            set { SetPoint(1, value); }
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("{0}3", "PointOnCircle")]
        public PointBase PointOnCircle3
        {
            get { return InPoints[2]; }
            set { SetPoint(2, value); }
        }
        public override void ComponentBuilding(Base component)
        {
            if (PointOnCircle2 == null) return;
            ske.Center = Utility.GetCenter(PointOnCircle1.ToPointD(), PointOnCircle2.ToPointD(),
                Pad.PxyToXY(SpareCoordinate));
            pske.Center = Pad.XYToPxy(Center);
            Radius = Center.DistanceTo(PointOnCircle1.ToPointD());
            ShowStatus($"{Resources.Translate("Creating")}{Resources.ColonSeparate}{Name}{Resources.LeftParenthesis}{string.Format(Resources.Translate("Of"), Resources.Translate("PointOnCircle") + "3", "Coordinate")}{Resources.ColonSeparate}{Pad.PxyToMath(SpareCoordinate)}");
            base.ComponentBuilding(component);
        }
        public override PointD NewPCenter
        {
            get
            {
                return Pad.XYToPxy(NewCenter);
            }
        }
        public override PointD NewCenter
        {
            get
            {
                return Utility.GetCenter(PointOnCircle1.NewXY, PointOnCircle2.NewXY, PointOnCircle3.NewXY);
            }
        }
        public override double NewRadius
        {
            get
            {
                return NewCenter.DistanceTo(PointOnCircle1.NewXY);
            }
        }
        public override bool PointsHaveConstraint
        {
            get { return false; }
        }
        public override bool CanDrawVirtual
        {
            get
            {
                if (PointOnCircle2 != null)
                {
                    return true;
                }
                return false;
            }
        }
        //public override void ComponentChanged(ShapeBase component)
        //{
        //    ske.Center = NewCenter;
        //    pske.Center = NewPCenter;
        //    Radius = NewRadius;
        //    base.ComponentChanged(component);
        //}
        public override void CreateComponents()
        {
            if (PointOnCircle1 == null)
            {
                PointOnCircle1 = Pad.CreatePoint($"{Resources.Translate("PointOnCircle")}1", SpareCoordinate_);
                Pad.TrackObj = this;
            }
            else if (PointOnCircle2 == null)
            {
                PointOnCircle2 = Pad.CreatePoint($"{Resources.Translate("PointOnCircle")}2", SpareCoordinate_, true, new PointCollectionBase[] { new Universal(PointOnCircle1.ToPointD()) });
            }
            else
            {
                PointOnCircle3 = Pad.CreatePoint($"{Resources.Translate("PointOnCircle")}3", SpareCoordinate_, true, new PointCollectionBase[] { new Universal(PointOnCircle1.ToPointD(), PointOnCircle1.GetSlope(PointOnCircle2)) });
                base.CreateComponents();
            }
        }
    }
}
